#include "includes.h"


#ifdef USE_SPI
sbit CS = P1^1;
void SpiInit(void)
{
	P1M0 |= 0x01;							//Set P1^1 as output
	CS = 1;
	PSFT |= 0x80;							//Shift SPI to MOSI(P10)/SCSB(P11)/MISO(P12)/SCK(P13)
	SPCON = 0xF1;							//Master mode
}

void SpiWrReg(uint8_t addr, uint8_t dat)
{
	uint8_t tmp;
	
	CS = 0;
	SPDAT = addr;
	while((SPSTA&0x80)==0)
	tmp = SPSTA;								//Clear SPIF
	SPDAT = dat;
	while((SPSTA&0x80)==0)
	tmp = SPSTA;								//Clear SPIF
	CS = 1;
}

uint8_t SpiRdReg(uint8_t addr)
{
	uint8_t tmp;
	uint8_t dat;
	
	CS = 0;
	SPDAT = addr;
	while((SPSTA&0x80)==0)
	tmp = SPSTA;								//Clear SPIF
	SPDAT = 0x00;
	while((SPSTA&0x80)==0)
	tmp = SPSTA;								//Clear SPIF
	dat = SPDAT;
	CS = 1;
	return dat;
}
#endif	//USE_SPI


